package leo.manager
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;
	
	import leo.data.Config;
	import leo.data.GameModel;
	import leo.game.ui.UIMainLoading;
	/**
	 * 加载器管理器 
	 * @author leo
	 * 
	 */
	public class LoaderManager
	{
		/**
		 * 当前链接数 
		 */		
		private var index :int = 0;
		/**
		 * 总链接数 
		 */		
		private var total :int;
		/**
		 * 是否在loading中 
		 */		
		private var isLoad :Boolean;
		/**
		 * 图片链接地址 
		 */		
		private var urlList :Array;
		/**
		 * loader列表 
		 */		
		private var loaderList :Array;
		/**
		 * 当前图片的名字，用来作索引 
		 */		
		private var curImgName :String;
		/**
		 * 加载完毕后的回调函数 
		 */		
		private var callBack :Function;
		private static var _instance :LoaderManager;
		public function LoaderManager()
		{
		}
		
		public function loadList(list :Array, cb :Function = null):void
		{
			if(!list || list.length == 0) return;
			
			this.urlList = list;
			this.callBack = cb;
			this.total = list.length - 1;
			this.loaderList = [];
			var path :String = this.urlList.shift();
			startLoad(path);
		}
		private function startLoad(url :String):void
		{
			this.isLoad = true;
			var start :int = url.lastIndexOf("/") + 1;
			var end :int = url.lastIndexOf(".");
			var name :String = url.substring(start,end);
			this.curImgName = name;
			var imgLoader :Loader = new Loader();
			imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imgLoaderHandler);
			imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadErrorHandler);
			if(url.lastIndexOf(".swf") == -1)
			{
				imgLoader.load(new URLRequest(url));
			}else
			{
				var loaderContext :LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
				imgLoader.load(new URLRequest(url),loaderContext);
			}
			this.loaderList.push(imgLoader);
		}
		
		private function imgLoaderHandler(event :Event = null):void
		{
			var target :DisplayObject = (event.currentTarget as LoaderInfo).content;
			if(target is Bitmap)
			{
				var bitmap :Bitmap = target as Bitmap;
				GameModel.instance.imgDict[this.curImgName] = bitmap.bitmapData;
			}
			this.isLoad = false;
			nextLoad();
			this.index++;
		}
		private function nextLoad():void
		{
			if(this.urlList.length > 0)
			{
				var path :String = this.urlList.shift();
				startLoad(path);
			}else
			{
				this.urlList = null;
				this.isLoad = false;
			}
			checkLoadComplete();
		}
		private function checkLoadComplete():void
		{
			if(this.total == this.index  && this.isLoad == false)
			{
				if(this.callBack != null)
				{
					this.callBack();
				}
				for(var i :int = this.loaderList.length - 1; i >= 0 ; i--)
				{
					(this.loaderList[i] as Loader).removeEventListener(Event.COMPLETE,imgLoaderHandler);
					this.loaderList[i] = null;
					this.loaderList.splice(i,1);
				}
				this.loaderList = null;
				trace("img load complete");
			}
			var value :int = (this.index/this.total) * 100;
			UIMainLoading.instance.percent = value;
		}
		
		
		private function loadErrorHandler(e :Event):void
		{
			var url :String = ((e.target) as LoaderInfo).loaderURL;
			trace("loadErrorHandler " + url);
			((e.currentTarget) as EventDispatcher).removeEventListener(IOErrorEvent.IO_ERROR,loadErrorHandler);
		}

		public static function get instance():LoaderManager
		{
			if(_instance == null)
			{
				_instance = new LoaderManager();
			}
			return _instance;
		}


	}
}

class privateClass{}